package controller

import (
	"github.com/labstack/echo"
	"github.com/qiniu/api.v7/v7/storage"
	uuid "github.com/satori/go.uuid"
	"net/http"
	"src/config"
	"src/model"
	"src/util/context"
	"src/util/time"
	"src/util/token"
	"strings"
)

func Login(c echo.Context) error {
	c.Response().Header().Set("Access-Control-Allow-Origin", "*")
	c.Response().Header().Set("Access-Control-Allow-Methods", "GET,POST")
	c.Response().Header().Set("headers", "Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers, Cache-Control, Content-Language, Content-Type")
	c.Response().Header().Set("Access-Control-Allow-Credentials", "true")
	c.Response().Header().Set("Access-Control-Expose-Header", "Origin, X-Requested-With, Content-Type, Accept, Authorization")

	secret := LoginParam{}
	_ = c.Bind(&secret)
	if secret.Secret != config.LoginSecret {
		return context.RetError(c, http.StatusBadRequest, http.StatusBadRequest, "密码错误")
	}
	jwtAuth := map[string]interface{}{
		"time": time.GetSecondTimestamp(),
	}
	jwtToken := token.GetJWTToken(jwtAuth)
	return context.RetData(c, jwtToken)
}

func Kuayuan(c echo.Context) error {
	c.Response().Header().Set("Access-Control-Allow-Origin", "*")
	c.Response().Header().Set("Access-Control-Allow-Methods", "GET,POST")
	c.Response().Header().Set("headers", "Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers, Cache-Control, Content-Language, Content-Type")
	c.Response().Header().Set("Access-Control-Allow-Credentials", "true")
	c.Response().Header().Set("Access-Control-Expose-Header", "Origin, X-Requested-With, Content-Type, Accept, Authorization")
	return c.String(200, "")
}

func GetQiniuImgUpToken(c echo.Context) error {
	data := TypeParam{}
	err := c.Bind(&data)
	if err != nil {
		return context.RetError(c, http.StatusBadRequest, http.StatusBadRequest, config.ErrorMsgParamWrong)
	}
	if data.Num < 1 {
		data.Num = 1
	}
	imgID := []string{}
	for i := 0; i < data.Num; i++ {
		a := uuid.NewV4().String()
		imgID = append(imgID, a)
	}

	suffix := data.Suffix
	if suffix == "" {
		suffix = config.ImgSuffix
	}

	imgPrefix, ok := config.ImgPrefix[data.Type]
	if !ok {
		return context.RetError(c, http.StatusBadRequest, http.StatusBadRequest, config.ErrorMsgParamWrong)
	}
	microImgPrefix := config.ImgPrefixMicro[data.Type]

	RES := []Qiniu{}

	for i := 0; i < data.Num; i++ {
		keyToOverwrite := imgPrefix + imgID[i] + suffix
		saveAsKey := microImgPrefix + imgID[i] + suffix

		fop := config.ImgOps + "|saveas/" + storage.EncodedEntry(config.Conf.Qiniu.Bucket, saveAsKey)
		persistentOps := strings.Join([]string{fop}, ";")
		upToken := token.GetCustomUpToken(keyToOverwrite, persistentOps, config.TokenQiniuExpire)

		img := model.Img{
			URL:      config.ImgURIPrefix + keyToOverwrite,
			MicroURL: config.ImgURIPrefix + saveAsKey,
		}
		a := Qiniu{
			Upload_token: upToken,
			Key:          keyToOverwrite,
			Img:          img,
		}
		RES = append(RES, a)
	}
	resData := map[string]interface{}{}
	resData["QiniuUps"] = RES
	return context.RetData(c, resData)
}
